#ifndef _QR_CODE_H
#define _QR_CODE_H

extern void rotFunGoBack();

static lv_obj_t		*uiQrcode;
static lv_label_t	*uiQrContentLabel;
static lv_obj_t		*uiQr;
static const int uiQrcodeWidth = CONFIG_SCREEN_WIDTH - 40;
static const int uiQrcodeHeight = CONFIG_SCREEN_HEIGHT - 40;

void hideSettingsQrcode() {
	lv_obj_add_flag(uiQrcode, LV_OBJ_FLAG_HIDDEN);
	rotFunGoBack();
}

void showSettingsQrcodeTask() {
	char showContent[128] = {0};
	sprintf(showContent, "连接SSID为%s的WIFI后扫描二维码进行设置", stGroup[ST_SSID].strValue);
	lv_label_set_text(uiQrContentLabel, showContent);
	char QrUrl[64] = {0};
	sprintf(QrUrl, "http://%s", "192.168.0.101");
	lv_qrcode_update(uiQr, QrUrl, strlen(QrUrl));
	lv_obj_clear_flag(uiQrcode, LV_OBJ_FLAG_HIDDEN);
	rotFunSet(NULL, NULL, &hideSettingsQrcode);
	vTaskDelete(NULL);
}

void qrCodeInit() {
	uiQrcode = lv_obj_create(root);
	lv_obj_add_flag(uiQrcode, LV_OBJ_FLAG_HIDDEN);
	lv_obj_remove_style_all(uiQrcode);
	lv_obj_set_size(uiQrcode, CONFIG_SCREEN_WIDTH, CONFIG_SCREEN_HEIGHT);
	lv_obj_set_style_bg_color(uiQrcode, LV_COLOR_BLACK, 0);
	lv_obj_set_style_bg_opa(uiQrcode, LV_OPA_60, 0);
	lv_obj_center(uiQrcode);

	lv_obj_t *uiQrBody = lv_obj_create(uiQrcode);
	lv_obj_remove_style_all(uiQrBody);
	lv_obj_set_size(uiQrBody, uiQrcodeWidth, uiQrcodeHeight);
	lv_obj_set_style_bg_color(uiQrBody, LV_COLOR_WHITE, 0);
	lv_obj_set_style_bg_opa(uiQrBody, LV_OPA_COVER, 0);
	lv_obj_center(uiQrBody);
	lv_obj_set_style_radius(uiQrBody, 10, 0);

	lv_obj_t *uiQrN = lv_obj_create(uiQrBody);
	lv_obj_remove_style_all(uiQrN);
	lv_obj_set_size(uiQrN, uiQrcodeWidth - 20, LV_SIZE_CONTENT);
	lv_obj_center(uiQrN);
	lv_obj_set_style_layout(uiQrN, LV_LAYOUT_FLEX, 0);
	lv_obj_set_style_flex_flow(uiQrN, LV_FLEX_FLOW_COLUMN, 0);
	lv_obj_set_style_flex_main_place(uiQrN, LV_FLEX_ALIGN_CENTER, 0);
	lv_obj_set_style_flex_cross_place(uiQrN, LV_FLEX_ALIGN_CENTER, 0);
	
	uiQrContentLabel = lv_label_create(uiQrN);
	lv_obj_set_size(uiQrContentLabel, uiQrcodeWidth - 30, LV_SIZE_CONTENT);
	lv_obj_set_style_text_font(uiQrContentLabel, CFONT, 0);
	lv_obj_set_style_text_color(uiQrContentLabel, LV_COLOR_FONT_1, 0);
	lv_obj_set_style_text_align(uiQrContentLabel, LV_TEXT_ALIGN_LEFT, 0);
	lv_label_set_long_mode(uiQrContentLabel, LV_LABEL_LONG_WRAP);
	lv_obj_set_align(uiQrContentLabel, LV_ALIGN_LEFT_MID);
	lv_obj_set_style_pad_bottom(uiQrContentLabel, 10, 0);
	lv_label_set_text(uiQrContentLabel, "");

	uiQr = lv_qrcode_create(uiQrN, 100, LV_COLOR_MAIN_BG_1, LV_COLOR_WHITE);
	lv_obj_align(uiQr, LV_ALIGN_TOP_MID, 0, 70);
}

void qrCodeInitTask() {
	qrCodeInit();
	vTaskDelete(NULL);
}

#endif